package org.example.demo7.tang_demo7_model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DBtest {
    String name;
    String pass;

    public void setInfo(String username, String password) {
        this.name = username;
        this.pass = password;
    }

    public void userlogin(){
        String url = "jdbc:mysql://localhost:3306/library_management?useSSL=false&serverTimezone=Asia/Shanghai";
        String username = "root";
        String password = "666666zzzzzz";

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

            try {
                //0. 加载数据库驱动
                Class.forName("com.mysql.jdbc.Driver");

                // 1. 获取数据库连接
                conn = DriverManager.getConnection(url, username, password);

                // 2. 定义SQL语句，使用 ? 作为占位符
                String sql = "SELECT id, username, name, role, status, email FROM user WHERE username = ? and  password = ?";

                // 3. 创建 PreparedStatement 对象
                pstmt = conn.prepareStatement(sql);

                // 4. 为占位符设置值 (索引从 1 开始)
                pstmt.setString(1, username);
                pstmt.setString(2, password);

                // 5. 执行查询，获取结果集 ResultSet
                rs = pstmt.executeQuery();

                // 6. 处理结果集
                if (rs.next()) {
                    // 如果查询到数据，从 ResultSet 中获取信息
                    int id = rs.getInt("id");
                    String name = rs.getString("name");
                    String role = rs.getString("role");
                    int status = rs.getInt("status");
                    String email = rs.getString("email");

                    // 打印查询到的用户信息
                    System.out.println("查询成功！找到用户:");
                    System.out.println("ID: " + id);
                    System.out.println("Username: " + username);
                    System.out.println("Name: " + name);
                    System.out.println("Role: " + role);
                    System.out.println("Status: " + (status == 1 ? "正常" : "禁用"));
                    System.out.println("Email: " + email);
                } else {
                    // 如果没有查询到数据
                    System.out.println("查询失败！未找到用户名为 '" + username + "' 的用户。");
                }

            } catch (Exception e) {
                System.err.println("查询操作失败！");
                e.printStackTrace();
            } finally {
                // 7. 关闭资源 (在实际开发中必须执行)
                try {
                    if (rs != null) rs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    if (pstmt != null) pstmt.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    if (conn != null) conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
}

